//
//  ViewController.swift
//  KZImage
//
//  Created by 千山我独行 on 16/3/15.
//  Copyright © 2016年 千山我独行. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var count: Int = 0
    
    // 图片数据源
    var array = [UIImage]()
    
    // 视图数组
    var imageViews: [UIImageView] = [UIImageView]()
    
    // 模式背景视图
    lazy var bgImageView: UIImageView = UIImageView(frame: self.view.bounds)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    private func setupUI() {
        
        // 数据源
        let path = NSBundle.mainBundle().pathForResource("Image.plist", ofType: nil)
        let tempArray = NSArray(contentsOfFile: path!)! as! [String]
        for value in tempArray {
            array.append(UIImage(named: value)!)
        }
        
        // MARK: - 磨砂背景
        bgImageView.image = array[0].applyExtraLightEffect()
        view.addSubview(bgImageView)
        
        let imageView1 = addChildImageView(width: 220, centerY: 240)
        let imageView2 = addChildImageView(width: 210, centerY: 220)
        let imageView3 = addChildImageView(width: 200, centerY: 200)
        
        view.addSubview(imageView3)
        view.addSubview(imageView2)
        view.addSubview(imageView1)
        
    }
    
    // MARK: - 点击屏幕时执行事件
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        self.view.userInteractionEnabled = false
        
        if count >= array.count {
            if count > array.count + 1 {
                print("图片已取完")
                return
            }
            moveImageView()
            
            return
        }
        
        let image: UIImage = array[count]
        
        
        // 开始点击时，就去设置磨砂视图、取出下一张图片设置为当前 磨砂 视图
        dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
            
            let bgImage = self.imageViews[1].image!.applyExtraLightEffect()
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                self.bgImageView.image = bgImage
                
            })
        })
        
        // 取出最上层的控件
        let frontImageView = self.imageViews[0]
        
        
        // 动画移动第一个控件
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            /// 将控件移除屏幕
            frontImageView.frame.origin.y = zSHeight
            
            }) { (_) -> Void in
                
                // 设置图片
                frontImageView.image = image
                
                // 1、将上层控件从数组中移除、再添加到控件组末尾（每次都操作首控件，后面控件依次执行）
                self.imageViews.removeFirst()
                self.imageViews.append(frontImageView)
                
                // 2、将对应的控件设置到视图层底部
                frontImageView.frame.size = CGSizeMake(200, 200)
                frontImageView.center = CGPointMake(self.view.center.x, 220)
                
                self.view.exchangeSubviewAtIndex(5, withSubviewAtIndex: 4)
                self.view.exchangeSubviewAtIndex(4, withSubviewAtIndex: 3)
                
                // 3、动画调整后两个控件 frame
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    frontImageView.frame.size = CGSizeMake(200, 200)
                    frontImageView.center = CGPointMake(self.view.center.x, 200)
                    
                    self.imageViews[0].frame.size = CGSizeMake(220, 220)
                    self.imageViews[0].center = CGPointMake(self.view.center.x, 240)
                    
                    self.imageViews[1].frame.size = CGSizeMake(210, 210)
                    self.imageViews[1].center = CGPointMake(self.view.center.x, 220)
                    
                    }) { (_) -> Void in
                        // 动画结束后、开启用户交互
                        self.view.userInteractionEnabled = true
                }
        }
        count++
        
    }
    
    
    // 移除图片
    private func moveImageView() {
        
        print(self.count)
        
        // 开始点击时，就去设置磨砂视图、取出下一张图片设置为当前 磨砂 视图
        dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
            
            let bgImage = self.imageViews[1].image!.applyExtraLightEffect()
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                self.bgImageView.image = bgImage
                
            })
        })
        
        // 取出最上层的控件
        let frontImageView = self.imageViews[0]
        
        
        // 动画移动第一个控件
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            /// 将控件移除屏幕
            frontImageView.frame.origin.y = zSHeight
            
            
            }) { (_) -> Void in
                
                // 1、将上层控件从数组中移除、再添加到控件组末尾（每次都操作首控件，后面控件依次执行）
                self.imageViews.removeFirst()
                self.imageViews.append(frontImageView)
                
                
                // 3、动画调整后两个控件 frame
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    if self.count == self.array.count {
                        self.imageViews[0].frame.size = CGSizeMake(220, 220)
                        self.imageViews[0].center = CGPointMake(self.view.center.x, 240)
                        self.imageViews[1].frame.size = CGSizeMake(210, 210)
                        self.imageViews[1].center = CGPointMake(self.view.center.x, 220)
                    } else {
                        self.imageViews[0].frame.size = CGSizeMake(220, 220)
                        self.imageViews[0].center = CGPointMake(self.view.center.x, 240)
                    }
                    }) { (_) -> Void in
                        // 动画结束后、开启用户交互
                        self.view.userInteractionEnabled = true
                    }
        }
        
        count++
    }
    
    // MARK: - 添加视图控件
    private func addChildImageView(width width: CGFloat, centerY: CGFloat) -> UIImageView {
        let imageView = UIImageView()
        
        // 添加到 视图数组
        imageViews.append(imageView)
        
        // 设置 frame
        imageView.frame.size = CGSizeMake(width, width)
        imageView.center = CGPointMake(self.view.center.x, centerY)
        
        // 设置 layer
        imageView.layer.borderWidth = 10
        imageView.layer.cornerRadius = 8
        imageView.layer.masksToBounds = true
        imageView.layer.borderColor = UIColor.whiteColor().CGColor
        
        // 设置图片
        let image = array[count]
        count++
        imageView.image = image
        
        return imageView
    }
    
}